import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'

Vue.use(VueRouter)
import errorpage from '@/views/errorpage/index'

const routes = [
  ...errorpage,
  {
    path: '/main',
    name: 'main',
    redirect:'main/home',
    component: () => import('@/views/Main/main.vue'),
    children:[
      {
        path:'home',
        name:'home',
        component:()=>import('@/views/Home/home.vue')
      },
      {
        path:'search',
        name:'search',
        component:()=>import('@/views/Home/search.vue')
      },
      {
        path:'list',
        name:'list',
        component:()=>import('@/views/List/list.vue'),
      },
      {
        path:'gift',
        name:'gift',
        component:()=>import('@/views/Gift/gift.vue')
      },
      {
        path:'cart',
        name:'cart',
        component:()=>import('@/views/Cart/cart.vue')
      },
      {
        path:'mine',
        name:'mine',
        component:()=>import('@/views/Mine/mine.vue')
      },
     
    ],
  },
 
  {
    path:'/cash',
    name:'cash',
    component:()=>import('@/views/cash/cash.vue'),
  },
  {
    path:'/address',
    name:'address',
    component:()=>import('@/views/address/address.vue'),
  },
  {
    path:'/addadd',
    name:'addadd',
    component:()=>import('@/views/address/addadd.vue'),
  },
  {
    path:'/goodslist',
    name:'goodslist',
    component:()=>import('@/views/List/goodslist.vue'),
  },
  {
    path:'/goodsdetail',
    name:'goodsdetail',
    component:()=>import('@/views/detail/goodsdetail.vue'),
  },
  {
    path: '/',
    name: 'guide',
    component: () => import('@/views/guide/guide.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/user/login.vue')
  },
  {
    path: '/codelogin',
    name: 'codelogin',
    component: () => import('@/views/user/codelogin.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/user/register.vue')
  },
  {
    path: '/information',
    name: 'information',
    component: () => import('@/views/information/info.vue')
  },
  {
    path: '*',
    redirect:"/404"
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

//main路由守卫
router.beforeEach((to,from,next)=>{
  var user = localStorage.getItem('mobile');
  if(to.name=='cart'||to.name=='mine'){
    if(!user){
        // next({name:'login'});
        if (typeof to.query._t !== "undefined") {
          next({name:'login'});
        } else {
          to.query._t = new Date().getTime().toString();
          next({name:'login'});
        }
    }else{
      // next();
            if (typeof to.query._t !== "undefined") {
              next();
            } else {
              to.query._t = new Date().getTime().toString();
              next(to);
            }
    }
  }else{
    // next();
        if (typeof to.query._t !== "undefined") {
          next();
        } else {
          to.query._t = new Date().getTime().toString();
          next(to);
        }
  }
  
})


const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}


export default router
